package com.demo.service.dataManagement;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * @Classname PoetService
 * @Description TODO
 * @Date 2020/3/13 17:16
 * @Created by yuanhang
 */
public class PoetService {
    /**
     * @return
     * @throws
     * @description 条件查询
     * @author yuanhang
     * @date 2020/3/13 17:18
     * @Param
     */
    public Page<Record> condition(String name, int page, int limit) {
        Kv cond = Kv.by("name", name);
        Page<Record> recordList = Db.template("poet.condition", cond).paginate(page, limit);
        //增加序号、整理出生日期、死亡日期
        for (int i = 0; i < recordList.getList().size(); i++) {
            Record record = recordList.getList().get(i);
            //序号
            record.set("index", i + 1);
            //出生日期
            String birthDate = "";
            Integer birthYear = record.getInt("birth_year");
            if (birthYear != null) {
                if (birthYear > 0) {
                    //公元默认不显示
                    birthDate = birthYear + "年";
                } else {
                    int year = 0 - birthYear.intValue();//取正数
                    birthDate = "公元前" + year + "年";
                }
            }
            Integer birthMonth = record.getInt("birth_month");
            if (birthMonth != null && (birthMonth > 0)) {
                birthDate = birthDate + birthMonth + "月";
            }
            Integer birthDay = record.getInt("birth_day");
            if (birthDay != null && (birthDay > 0)) {
                birthDate = birthDate + birthDay + "日";
            }
            record.set("birth_date", birthDate);

            //死亡日期
            String deathDate = "";
            Integer deathYear = record.getInt("death_year");
            if (deathYear != null) {
                if (deathYear > 0) {
                    //公元默认不显示
                    deathDate = deathYear + "年";
                } else {
                    int year = 0 - deathYear.intValue();//取正数
                    deathDate = "公元前" + year + "年";
                }
            }
            Integer deathMonth = record.getInt("death_month");
            if (deathMonth != null && (deathMonth > 0)) {
                deathDate += deathMonth + "月";
            }
            Integer deathDay = record.getInt("death_day");
            if (deathDay != null && (deathDay > 0)) {
                deathDate += deathDay + "日";
            }
            record.set("death_date", deathDate);
        }
        return recordList;
    }
}
